import axios from "axios";
import qs from "qs";
import { Message } from "element-ui";

// 初始化Axios实例
const Axios = axios.create({
  timeout: 10000,
  responseType: "json",
  withCredentials: true
});
// 请求拦截
Axios.interceptors.request.use(
  function (config) {
    // application/x-www-form-urlencoded格式默认设置
    if (
      config.headers["content-type"] &&
      config.headers["content-type"].toLowerCase().trim() ===
      "application/x-www-form-urlencoded"
    ) {
      config.method = "post";
      if (config.data && typeof config.data === "object") {
        config.data = qs.stringify(config.data);
      }
    }
    return config;
  },
  function (error) {
    let message = `请求错误：${error.message}`;
    // 判断请求超时
    if (
      error.code === "ECONNABORTED" &&
      error.message.indexOf("timeout") !== -1
    ) {
      message = "请求超时，请重试！";
    }
    Message({
      type: "error",
      message: message
    });
    return Promise.reject(error);
  }
);

// 响应拦截
Axios.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    if (error && error.response) {
      let message = "";
      switch (error.response.status) {
        default:
        case 400:
          error.message = "请求错误";
          message = "请求错误";
          break;
        case 401:
          error.message = "未授权，请登录";
          message = "未授权，请登录";
          break;
        case 403:
          error.message = "拒绝访问";
          message = "拒绝访问";
          break;
        case 404:
          error.message = "请求服务或资源不存在";
          message = "请求服务或资源不存在";
          break;
        case 408:
          error.message = "请求超时";
          message = "请求超时";
          break;
        case 500:
          error.message = "服务器内部错误";
          message = "服务器内部错误";
          break;
        case 501:
          error.message = "服务未实现";
          message = "服务未实现";
          break;
        case 502:
          error.message = "网关错误";
          message = "网关错误";
          break;
        case 503:
          error.message = "服务不可用";
          message = "服务不可用";
          break;
        case 504:
          error.message = "网关超时";
          message = "网关超时";
          break;
        case 505:
          error.message = "HTTP版本不受支持";
          message = "HTTP版本不受支持";
          break;
      }
      Message({
        type: "error",
        message: `响应错误：${message}`
      });
    }

    return Promise.reject(error);
  }
);
/**
 * get请求方法
 *
 * @param {String} url 请求url
 * @param {Object} data 请求参数
 * @param {Object} config 请求配置，如headers，timeout等
 */
const get = (url, data, config) => {
  let axiosConf = {
    method: "get",
    params: data,
    url
  };
  if (config && typeof config === "object") {
    axiosConf = {
      ...axiosConf,
      ...config
    };
  }
  return Axios(axiosConf);
};
/**
 * post请求方法
 * @param {String} url 请求url
 * @param {Object} data 请求参数
 * @param {Object} config 请求配置，如headers，timeout等
 * ,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
 */
const post = (url, data, config) => {
  let axiosConf = {
    url: url,
    method: "post",
    data: data
  };
  if (config && typeof config === "object") {
    axiosConf = {
      ...axiosConf,
      ...config
    };
  }
  return Axios(axiosConf);
};

export { get, post };
